// pages/control/insure_ucharts/insure_ucharts.js
const http = require("../../../utils/request");

Component({
  

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    //summaryData
    productionSummary:[{  
      title:"总共(个)",
      info:"8万"
    },{
      title:"占比",
      info:"30%"
    }],    
    photoSummary:[{  
      title:"总共(次)",
      info:"加载中"
    },{
      title:"平均(次)",
      info:"加载中"
    }],
    //controlCard组件
    defualtOptions:0,
    pickerLabel:['近50批','近100批','近1000批'],
    pickerValue:['50','100','1000'],
    pickerOptions:[
      { label: '近50批', value: '近50批' },
      { label: '近100批', value: '近100批' },
      { label: '近1000批', value: '近1000批' }
    ],
    pickerTitle:"选择图表周期",
    photoSummaryInfoList:[{}],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //card组件
    receiveTapPicker(e){
      console.log(e.detail);
      console.log('选择了：'+e.detail.pickerValue[0])
      let photoSummaryInfoListIndex=this.data.pickerValue.indexOf(this.extractNumberFromText(e.detail.pickerValue[0]))
      this.updatePhotoSummaryFromList(photoSummaryInfoListIndex)
    },
    fetchData(){
      this.data.pickerOptions.forEach((item,index)=>{
        http.getRequest('/admin/console/verification?count='+this.data.pickerValue[index],{},(res)=>{
          console.log(res);
          this.setData({
            ['photoSummaryInfoList[' + index + ']']:{...res.data.data}
          })
          if(index===this.data.defualtOptions){
            this.updatePhotoSummaryFromList(index)
          }
        })
      })
    },
    updatePhotoSummaryFromList(index){
      console.log(index);
      this.setData({
        [`photoSummary[0].info`]:this.data.photoSummaryInfoList[index].totalVerificationCountForCultivations,
        [`photoSummary[1].info`]:this.data.photoSummaryInfoList[index].averageVerificationCountForCultivations
      })
    },
    extractNumberFromText(text) {
      const regex = /\d+/g;
      const matches = text.match(regex);
      return matches ? matches[0] : null;
    }
  },
  lifetimes: {
    ready(){
      http.getRequest('/admin/console/insurance',{},res =>{
        console.log('in',res.data.data);
        let productionSummary = [{  
          title:"总共(个)",
          info: res.data.data.totalEnclosureNumber
        },{
          title:"占比",
          info: (res.data.data.insuranceRate *100).toFixed(2)+"%"
        }]
        this.setData({
          productionSummary,
        })
      })
    },
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.fetchData()
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})